package com.qfedu.yuxiaoer.service.impl;

import com.qfedu.yuxiaoer.dto.BillQueryParam;
import com.qfedu.yuxiaoer.entity.*;
import com.qfedu.yuxiaoer.mapper.BillCostItemMapper;
import com.qfedu.yuxiaoer.mapper.BillCostItemRelationMapper;
import com.qfedu.yuxiaoer.mapper.BillMapper;
import com.qfedu.yuxiaoer.service.BillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BillServiceImpl implements BillService {

    @Autowired
    private BillMapper billMapper;

    @Autowired
    private BillCostItemMapper billCostItemMapper;

    @Autowired
    private BillCostItemRelationMapper billCostItemRelationMapper;

    @Override
    public Bill selectBillDetailsById(Integer id) {
        return billMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Bill> selectBillList(BillQueryParam queryParam) {
        BillExample billExample = new BillExample();
        BillExample.Criteria criteria = billExample.createCriteria();

        //收支账单类型不为null，加入添加条件
        if (queryParam.getReceivePayTyp() != null){
            int receivePayTyp = queryParam.getReceivePayTyp();
            criteria.andReceivePayTypeEqualTo(receivePayTyp);
        }

        //账单状态不为null，加入添加条件
        if (queryParam.getStatus() != null){
            int status = queryParam.getStatus();
            criteria.andStatusEqualTo(status);
        }

        //部门不为null，加入添加条件
        if (queryParam.getDepartment() != null){
            String  department = queryParam.getDepartment();
            criteria.andDepartmentEqualTo(department);
        }

        //归属不为null，加入添加条件
        if (queryParam.getBelong() != null){
            int belong = queryParam.getBelong();
            criteria.andBelongEqualTo(belong);
        }

        //检索账单类型不为null，加入添加条件
        if (queryParam.getBillSearchTypeId() != null){
            int id  = queryParam.getBillSearchTypeId();
            criteria.andBillSearchTypeIdEqualTo(id);
        }

        //房源城市不为null，加入添加条件
        if (queryParam.getCity() != null){
            String city  = queryParam.getCity();
            criteria.andCityEqualTo(city);
        }

        //签约人不为null，加入添加条件
        if (queryParam.getSignatory() != null){
            String signatory = queryParam.getSignatory();
            criteria.andSignatoryEqualTo(signatory);
        }

        //出房管家不为null，加入添加条件
        if (queryParam.getLeaseButler() != null){
            String leaseButler = queryParam.getLeaseButler();
            criteria.andLeaseButlerEqualTo(leaseButler);
        }

        //审批状态不为null，加入添加条件
        if (queryParam.getApprovalStatus() != null){
            int approvalStatus = queryParam.getApprovalStatus();
            criteria.andApprovalStatusEqualTo(approvalStatus);
        }

        return billMapper.selectByExample(billExample);
    }

    @Override
    public int addCostItem(BillCostItem billCostItem,Integer billId) {
        int num = billCostItemMapper.insert(billCostItem);
        //获取bill_cost_item表的自增id
        int billCostItemId  = billCostItem.getId();
        //将账单表bill的id 和 获取到的 bill_cost_item表 的自增id封装进billCostItemRelation中
        BillCostItemRelation billCostItemRelation = new BillCostItemRelation();
        billCostItemRelation.setBillId(billId);
        billCostItemRelation.setBillCostItemId(billCostItemId);
        num = num + billCostItemRelationMapper.insert(billCostItemRelation);
        //两个表都插入成功了返回2
        return num;
    }

    @Override
    public int deleteCostItem(Integer billId,Integer billCostItemId) {
        //删除bill_cost_item表数据
        int num = billCostItemMapper.deleteByPrimaryKey(billCostItemId);

        //通过billId 和 billCostItemId 删除bill_cost_item_relation数据
        BillCostItemRelationExample billCostItemRelationExample = new BillCostItemRelationExample();
        BillCostItemRelationExample.Criteria criteria = billCostItemRelationExample.createCriteria();
        criteria.andBillIdEqualTo(billId);
        criteria.andBillCostItemIdEqualTo(billCostItemId);
        num = num + billCostItemRelationMapper.deleteByExample(billCostItemRelationExample);
        return num;
    }

}
